package bairui.international.weixin.portal.miniapp.controller.pay.order;

import java.math.BigDecimal;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lianjing.sh.spring.web.controller.base.action.Action;
import com.lianjing.sh.spring.web.controller.enhance.output.FomatOutput;
import com.sh_lianjing.wx.pay.api.Api_Pay_MiniApp;
import com.sh_lianjing.wx.pay.enums.TrafficWay;
import com.sh_lianjing.wx.pay.model.Model_UnifiedorderReturn;
import com.sh_lianjing.wx.pay.model.Model_UnifiedorderSendForMiniApp;

import bairui.international.model.domain.jpa.TB_ElectronicTicket;
import bairui.international.model.domain.jpa.TB_ProductOrder;
import bairui.international.model.domain.jpa.TB_Town;
import bairui.international.model.domain.jpa.TB_User;
import bairui.international.service.Service_ElectronicTicket;
import bairui.international.service.Service_ProductOrder;
import bairui.international.service.Service_User;
import bairui.international.weixin.portal.miniapp.config.Config_Weixin_App;
import lombok.Getter;
import lombok.Setter;

/**
 *
 * 预支付
 *
 * @generated
 * 
 * @see STest_C_Pay_Order_Prepare_Get 测试
 *
 */
@Controller
public class C_Pay_Order_Prepare_Get
 extends SC_Pay_Order_Prepare_Get 
{

	@Autowired private Config_Weixin_App config_Weixin_App;
	@Autowired private Service_ProductOrder service_ProductOrder;
	@Autowired private Service_User service_User;
	@Autowired private Service_ElectronicTicket service_ElectronicTicket;
	
	/**
	 * Get请求
	 */
	@Action(Get.class)
	@PostMapping(value=Get.RequestMappingValue, consumes=MediaType.APPLICATION_JSON_VALUE)
	public @FomatOutput @ResponseBody Object get(
		ModelMap map, HttpServletRequest request, 
		@PathVariable(name=Get.PathVariableName.OrderId) long orderId,
		@RequestBody Input input,
		@RequestParam String skey,
		@RequestParam(required=false) Long ticketId
	) throws Exception {
		TB_User user = service_User.findOne(it->it.skey.eq(skey));
		TB_ProductOrder order = service_ProductOrder.findById(orderId);
		// 处理优惠券
		TB_ElectronicTicket ticket = null;
		if(null != ticketId){
			ticket = service_ElectronicTicket.findById(ticketId);
		}
		// 处理积分
		order = service_ProductOrder.setScore(orderId, input.getScore(), ticket);
		BigDecimal totalFeeD = BigDecimal.valueOf(order.getPay())
				.multiply(BigDecimal.valueOf(100));
		// 设置数据
		InputAddress address = input.getAddress();
		service_ProductOrder.setOrderInfo(order, new TB_Town(address.getTownId()), address.getUsername(), address.getPhone(), address.getDetailAddress(), input.getComment());
		// 支付
		String outTradeNo = "order_" + order.getNumber();
		String openid = user.getOpenid();
		int totalFee = totalFeeD.intValue();
		if(totalFee == 0){
			service_ProductOrder.onLinePay(order.getNumber());
			//
			return "Finish";
		}
		/*
		 * 调用统一下单接口
		 */
		Model_UnifiedorderSendForMiniApp apiInput = new Model_UnifiedorderSendForMiniApp(
				config_Weixin_App, 
				openid,
				"订单支付", 
				outTradeNo, 
				totalFee,
				C_Pay_Order_Notify_Get.Get.Operate().getUrl()	// "/pay/order/notify"
			);
		apiInput.setAttach(order.getNumber().trim());
		Model_UnifiedorderReturn result = Api_Pay_MiniApp.unifiedorder(TrafficWay.Really, apiInput);
		if(result.isSuccess()){
			// 得到微信支付调用数据
			return result.gainRequestPaymentParam(config_Weixin_App);
		}else{
			return result.gainErrorData();
		}
	}
	
	public static class Input {
		@Getter @Setter private Double score;
		@Getter @Setter private String comment;
		@Getter @Setter private InputAddress address;
	}
	
	public static class InputAddress {
		@Getter @Setter Long townId;
		@Getter @Setter private Boolean defaultFlag;
		@Getter @Setter private String detailAddress;
		@Getter @Setter private String username;
		@Getter @Setter private String phone;
	}
	
}
